package cn.com.xinjinfu.pdoulcProduct.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.WindowManager;

public class NewUtil {
	public static final int TYPE_GET=0;
	public static final int TYPE_POST=1;
	public static final int CONNECTION_TIME_OUT=30*1000;
	public static final int TIME_OUT=30*1000;
	public static Map<String, Long> idmap;

	/**
	 * 将Drawable转化为Bitmap
	 * @param drawable
	 * @return
	 */
	public static Bitmap drawableToBitmap(Drawable drawable) {
		// 取 drawable 的长宽
		int w = drawable.getIntrinsicWidth();
		int h = drawable.getIntrinsicHeight();

		// 取 drawable 的颜色格式
		Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
				: Bitmap.Config.RGB_565;
		// 建立对应 bitmap
		Bitmap bitmap = Bitmap.createBitmap(w, h, config);
		// 建立对应 bitmap 的画布
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, w, h);
		// 把 drawable 内容画到画布中
		drawable.draw(canvas);
		return bitmap;
	}

	/**
	 * 获取屏幕分辨率
	 * @param context
	 * @return
	 */
	public static int[] getScreenDispaly(Context context) {
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		int width = windowManager.getDefaultDisplay().getWidth();// 手机屏幕的宽度
		int height = windowManager.getDefaultDisplay().getHeight();// 手机屏幕的高度
		int result[] = { width, height };
		return result;
	}

	/**
	 * 将Bitmap转化为Drawable
	 * @param bitmap
	 * @return
	 */
	public static Drawable bitmapToDrawable (Bitmap bitmap) {
		@SuppressWarnings("deprecation")
		BitmapDrawable drawable = new BitmapDrawable(bitmap);
		return drawable;
	}
	/**
	 * dp转化为px
	 * @param 
	 * @return
	 */
	public static int dip2px(Context context, float dpValue){
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
	/**
	 * px转化为dp
	 * @param 
	 * @return
	 */
	public static int px2dip(Context context, float pxValue){
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}
	/**
	 * 获得软件版本号
	 * @param 
	 * @return
	 */
	public static String getVersionName(Context context)  {
		PackageManager packageManager = context.getPackageManager();
		PackageInfo packInfo;
		try {
			packInfo = packageManager.getPackageInfo(
					context.getPackageName(), 0);
			return packInfo.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 判断字符串非空
	 * @param 
	 * @return
	 */
	public static boolean isnull(String s){
		if(s==null||s.trim().equals("null")||s.trim().equals("NULL")||s.trim().equals("")){
			return true;
		}
		return false;
	}

	/**
	 * httpclent联网
	 * @param 
	 * @return
	 */
	public static HttpEntity getEntity(String uri, 
			List<NameValuePair> pairs, int method) throws Exception{
		HttpEntity entity = null;
		BasicHttpParams httpParams = new BasicHttpParams();  
		HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIME_OUT);  
		HttpConnectionParams.setSoTimeout(httpParams, TIME_OUT);  
		HttpClient client = new DefaultHttpClient(httpParams);  
		//定义请求对象的变量
		HttpUriRequest request = null;
		switch (method) {
		case TYPE_GET: //如果发送的是get请求
			StringBuffer sb = new StringBuffer(uri);
			sb.append("?");
			for(int i=0; pairs!=null&&i<pairs.size(); i++){
				NameValuePair pair = pairs.get(i);
				sb.append(pair.getName()).append("=")
				.append(pair.getValue())
				.append("&");
			}
			sb.deleteCharAt(sb.length()-1);
			Log.i("uri", sb.toString());
			request=new HttpGet(sb.toString());
			break;
		case TYPE_POST: //发送post请求
			request = new HttpPost(uri);
			HttpPost post=(HttpPost)request;
			UrlEncodedFormEntity ent = 
					new UrlEncodedFormEntity(pairs, "utf-8");
			post.setEntity(ent);
//			Log.i("socket",ent.toString());
			break;
		}
		//把request请求对象发送出去
		HttpResponse res = client.execute(request);
		if (res.getStatusLine().getStatusCode() == 200) {
			entity = res.getEntity();
			return entity;
		}else{
			return null;
		}
	}

	/**
	 * 设置session并联网
	 * @param 
	 * @return
	 */
	public static HttpResponse getEntitySetCookie(String uri, 
			List<NameValuePair> pairs, int method) throws Exception{
		BasicHttpParams httpParams = new BasicHttpParams();  
		HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIME_OUT);  
		HttpConnectionParams.setSoTimeout(httpParams, TIME_OUT);  
		HttpClient client = new DefaultHttpClient(httpParams);  
		//定义请求对象的变量
		HttpUriRequest request = null;
		switch (method) {
		case TYPE_GET: //如果发送的是get请求
			//把namevaluepair中的参数组织起来，添加
			//到uri的后边?....
			//    sb = /server/login.do
			StringBuffer sb = new StringBuffer(uri);
			sb.append("?");
			//		    sb = /server/login.do?name=zs&pwd=1234&
			for(int i=0; pairs!=null&&i<pairs.size(); i++){
				NameValuePair pair = pairs.get(i);
				sb.append(pair.getName()).append("=")
				.append(pair.getValue())
				.append("&");
			}
			//sb = /server/login.do?name=zs&pwd=1234
			sb.deleteCharAt(sb.length()-1);
//			Log.i("uri", sb.toString());
			request=new HttpGet(sb.toString());
			break;
		case TYPE_POST: //发送post请求
			request = new HttpPost(uri);
			HttpPost post=(HttpPost)request;
			UrlEncodedFormEntity ent = 
					new UrlEncodedFormEntity(pairs, "utf-8");
			post.setEntity(ent);
			break;
		}
		//		if(MyApplication.SESSIONID!=null){
		//			request.setHeader("Cookie", "PHPSESSID=" + MyApplication.SESSIONID);
		//		}
		//把request请求对象发送出去
		HttpResponse res = client.execute(request);
		return res;
	}
	/**
	 * 判断点击事件是否指定间隔以外
	 * @param 
	 * @return
	 */
	public static boolean checkClick(int id){
		if(idmap==null){
			idmap=new HashMap<String, Long>();
		}
		long first=0;
		if(idmap.containsKey(id+"hehe")){
			first = Long.parseLong(idmap.get(id+"hehe").toString());
		}
		if(System.currentTimeMillis()-first>1500){
			if(idmap.containsKey(id+"hehe")){
				idmap.remove(id+"hehe");
			}
			idmap.put(id+"hehe", System.currentTimeMillis());
			return true;
		}
		return false;
	}



}
